package com.hunan.chinamobile.manager;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunan.chinamobile.core.BaseController;
import com.hunan.chinamobile.core.Result;
import com.hunan.chinamobile.core.ResultGenerator;
import com.hunan.chinamobile.model.City;
import com.hunan.chinamobile.model.User;
import com.hunan.chinamobile.service.CityService;
import com.hunan.chinamobile.service.HUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/city")
public class CityController extends BaseController{

    @Resource
    private CityService cityService;
    @Resource
    private HUserService hUserService;

    @PostMapping
    public Result add(@RequestBody City city){
        if(city == null || StringUtils.isEmpty(city.getCityName())){
            return ResultGenerator.genFailResult("参数为空！");
        }
        if(cityService.check(city.getCityName()) != null){
            return ResultGenerator.genFailResult("该城市已存在！");
        }
        city.setId(UUID.randomUUID().toString());
        city.setCreatedAt(new Date());
        city.setUpdatedAt(new Date());
        cityService.save(city);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping
    public Result list(@RequestParam(required = false) String name,
                       @RequestParam(name = "page", defaultValue = "1") Integer page,
                       @RequestParam(name = "size", defaultValue = "20") Integer size){
        PageHelper.startPage(page,size);
        List<City> cities = cityService.findByName(name);
        PageInfo<City> pageInfo = new PageInfo<>(cities);
        return ResultGenerator.genSuccessResult(pageInfo);
    }


    @PutMapping
    public Result update(@RequestBody City city){
        if(city == null || StringUtils.isEmpty(city.getCityName())){
            return ResultGenerator.genFailResult("参数为空！");
        }
        if(cityService.check(city.getCityName()) != null){
            return ResultGenerator.genFailResult("该城市已存在！");
        }
        city.setUpdatedAt(new Date());
        cityService.update(city);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    public Result del(@PathVariable String id){
        cityService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/user")
    public Result getUser(@RequestParam(required = false) String cityId,
                          @RequestParam(name = "page", defaultValue = "1") Integer page,
                          @RequestParam(name = "size", defaultValue = "20") Integer size){
        if(StringUtils.isEmpty(cityId)){
            return ResultGenerator.genFailResult("参数cityId为空!");
        }
        PageHelper.startPage(page,size);
        List<User> users = hUserService.findByCityId(cityId);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/all")
    public Result getAll(HttpServletRequest request, HttpServletResponse response){
        String cityId = getCityId(request, response);
        if(!StringUtils.isEmpty(cityId)) {
            return ResultGenerator.genSuccessResult(cityService.findById(cityId));
        }else {
            return ResultGenerator.genSuccessResult(cityService.findAll());
        }
    }
}
